#include <memory.h>
#include <stdio.h>
#include <sys/mman.h>

#include <fcntl.h>

#include <unistd.h>


int g_memFd = -1;
int *dmaBuff = NULL;
#define MAX_DMA_SIZE    1024
#define __REG(x)        (*((volatile unsigned int *)(x)))   
char devName[126];
int buf[4];
int  openMemDev()
{
    if( g_memFd == -1)
    {
        if((g_memFd = open(devName, O_RDWR | O_SYNC)) == -1)
        {
            printf("open /dev/mem error\n");
			return -1;
        }
    }
	return 0;
}


int mem_rw( char bRead,  unsigned int addr,  int value)  //init 
{

    if( openMemDev() < 0)
		return 0;

printf("open mem:%x for %c , value:%d\n", addr, bRead, value);

  if( bRead == 'r' || bRead == 'R')
	{
		for( ; value -- ;)
		{
			  buf[0] = addr;
		      read( g_memFd, &buf, 2);
			  printf("\nread value:0x%x\n", buf[0] );
			  addr  = addr   + 4;
		}
	}
  else
  	{ 
  		buf[0] = addr;
  		buf[1] = value;
  		write( g_memFd, &buf, 2);
  	}

return 0;
erroResult:
	printf("error: init \n" );
    return -1;
}

int main(int argc,char *argv[])
{
char bRead;
int value;
unsigned int addr;
if( argc < 2 )
{
	printf("name,dev (like:memdis /dev/mem) \n");
	return 0;
}
strcpy(devName , argv[1] );
printf(" enter r/w addr value for %s\n" , devName);
while(1 )
{
	printf("enter r/W\n");
fflush( stdin  );
   scanf("%c", &bRead  );
fflush( stdin );
printf(" enter   addr value \n");
 scanf("%x %x  ",  &addr, &value);
fflush( stdin  );
   if( mem_rw( bRead, addr, value) < 0)
      return;
} 


}

